package net.lovesrc.allin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.zhouzhou.fastjava.query.QueryArgs;
import com.zhouzhou.fastjava.query.QueryFilter;
import com.zhouzhou.fastjava.query.QuerySortor;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class ExtjsQueryArgs {

    private int start;

    private int limit;

    private String filters;

    private String sort;

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public String getFilters() {
        return filters;
    }

    public void setFilters(String filters) {
        this.filters = filters;
    }

    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public QueryArgs args() {
        QueryArgs args = new QueryArgs();
        JSONObject filterJsonObj = JSON.parseObject(getFilters());
        List<QueryFilter> filters = Lists.newArrayList();
        if (filterJsonObj != null) {
            filterJsonObj.forEach((r, v) -> {
                filters.add(new QueryFilter(r, v));
            });
        }
        args.setFilters(filters);
        if (StringUtils.isNotBlank(this.getSort())) {
            args.setSortors(JSON.parseArray(this.getSort(), QuerySortor.class));
        }
        args.setStart(start);
        args.setLimit(limit);
        return args;
    }

}
